home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / floatobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  17.0 KB  |  786 lines

  1. /* Float object implementation */
  2.  
  3. /* XXX There should be overflow checks here, but it's hard to check
  4.    for any kind of float exception without losing portability. */
  5.  
  6. #include "Python.h"
  7.  
  8. #include <ctype.h>
  9. #include "mymath.h"
  10. #include "protos/floatobject.h"
  11.  
  12. #ifdef i860
  13. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  14. #undef HUGE_VAL
  15. #endif
  16.  
  17. #if defined(HUGE_VAL) && !defined(CHECK)
  18. #define CHECK(x) if (errno != 0) ; \
  19.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  20.     else errno = ERANGE
  21. #endif
  22.  
  23. #ifndef CHECK
  24. #define CHECK(x) /* Don't know how to check */
  25. #endif
  26.  
  27. #ifdef HAVE_LIMITS_H
  28. #include <limits.h>
  29. #endif
  30.  
  31. #ifndef LONG_MAX
  32. #define LONG_MAX 0X7FFFFFFFL
  33. #endif
  34.  
  35. #ifndef LONG_MIN
  36. #define LONG_MIN (-LONG_MAX-1)
  37. #endif
  38.  
  39. #ifdef __NeXT__
  40. #ifdef __sparc__
  41. /*
  42.  * This works around a bug in the NS/Sparc 3.3 pre-release
  43.  * limits.h header file.
  44.  * 10-Feb-1995 bwarsaw@cnri.reston.va.us
  45.  */
  46. #undef LONG_MIN
  47. #define LONG_MIN (-LONG_MAX-1)
  48. #endif
  49. #endif
  50.  
  51. #if !defined(__STDC__) && !defined(macintosh)
  52. extern double fmod Py_PROTO((double, double));
  53. extern double pow Py_PROTO((double, double));
  54. #endif
  55.  
  56. #ifdef sun
  57. /* On SunOS4.1 only libm.a exists. Make sure that references to all
  58.    needed math functions exist in the executable, so that dynamic
  59.    loading of mathmodule does not fail. */
  60. double (*_Py_math_funcs_hack[])() = {
  61.     acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
  62.     fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
  63. };
  64. #endif
  65.  
  66. /* Special free list -- see comments for same code in intobject.c. */
  67. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  68. #define BHEAD_SIZE    8    /* Enough for a 64-bit pointer */
  69. #define N_FLOATOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
  70.  
  71. struct _floatblock {
  72.     struct _floatblock *next;
  73.     PyFloatObject objects[N_FLOATOBJECTS];
  74. };
  75.  
  76. typedef struct _floatblock PyFloatBlock;
  77.  
  78. static PyFloatBlock *block_list = NULL;
  79. static PyFloatObject *free_list = NULL;
  80.  
  81. static PyFloatObject *
  82. fill_free_list()
  83. {
  84.     PyFloatObject *p, *q;
  85.     /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
  86.     p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
  87.     if (p == NULL)
  88.         return (PyFloatObject *) PyErr_NoMemory();
  89.     ((PyFloatBlock *)p)->next = block_list;
  90.     block_list = (PyFloatBlock *)p;
  91.     p = &((PyFloatBlock *)p)->objects[0];
  92.     q = p + N_FLOATOBJECTS;
  93.     while (--q > p)
  94.         q->ob_type = (struct _typeobject *)(q-1);
  95.     q->ob_type = NULL;
  96.     return p + N_FLOATOBJECTS - 1;
  97. }
  98.  
  99. PyObject *
  100. #ifdef __SC__
  101. PyFloat_FromDouble(double fval)
  102. #else
  103. PyFloat_FromDouble(fval)
  104.     double fval;
  105. #endif
  106. {
  107.     register PyFloatObject *op;
  108.     if (free_list == NULL) {
  109.         if ((free_list = fill_free_list()) == NULL)
  110.             return NULL;
  111.     }
  112.     /* PyObject_New is inlined */
  113.     op = free_list;
  114.     free_list = (PyFloatObject *)op->ob_type;
  115.     PyObject_INIT(op, &PyFloat_Type);
  116.     op->ob_fval = fval;
  117.     return (PyObject *) op;
  118. }
  119.  
  120. PyObject *
  121. PyFloat_FromString(v, pend)
  122.     PyObject *v;
  123.     char **pend;
  124. {
  125.     extern double strtod Py_PROTO((const char *, char **));
  126.     const char *s, *last, *end;
  127.     double x;
  128.     char buffer[256]; /* For errors */
  129.     int len;
  130.  
  131.     if (PyString_Check(v)) {
  132.         s = PyString_AS_STRING(v);
  133.         len = PyString_GET_SIZE(v);
  134.     }
  135.     else if (PyUnicode_Check(v)) {
  136.         char s_buffer[256];
  137.  
  138.         if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
  139.             PyErr_SetString(PyExc_ValueError,
  140.                  "float() literal too large to convert");
  141.             return NULL;
  142.         }
  143.         if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), 
  144.                         PyUnicode_GET_SIZE(v),
  145.                         s_buffer, 
  146.                         NULL))
  147.             return NULL;
  148.         s = s_buffer;
  149.         len = strlen(s);
  150.     }
  151.     else if (PyObject_AsCharBuffer(v, &s, &len)) {
  152.         PyErr_SetString(PyExc_TypeError,
  153.                 "float() needs a string argument");
  154.         return NULL;
  155.     }
  156.  
  157.     last = s + len;
  158.     while (*s && isspace(Py_CHARMASK(*s)))
  159.         s++;
  160.     if (s[0] == '\0') {
  161.         PyErr_SetString(PyExc_ValueError, "empty string for float()");
  162.         return NULL;
  163.     }
  164.     errno = 0;
  165.     PyFPE_START_PROTECT("PyFloat_FromString", return 0)
  166.     x = strtod((char *)s, (char **)&end);
  167.     PyFPE_END_PROTECT(x)
  168.     /* Believe it or not, Solaris 2.6 can move end *beyond* the null
  169.        byte at the end of the string, when the input is inf(inity) */
  170.     if (end > last)
  171.         end = last;
  172.     while (*end && isspace(Py_CHARMASK(*end)))
  173.         end++;
  174.     if (*end != '\0') {
  175.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  176.         PyErr_SetString(PyExc_ValueError, buffer);
  177.         return NULL;
  178.     }
  179.     else if (end != last) {
  180.         PyErr_SetString(PyExc_ValueError,
  181.                 "null byte in argument for float()");
  182.         return NULL;
  183.     }
  184.     else if (errno != 0) {
  185.         sprintf(buffer, "float() literal too large: %.200s", s);
  186.         PyErr_SetString(PyExc_ValueError, buffer);
  187.         return NULL;
  188.     }
  189.     if (pend)
  190.         *pend = (char *)end;
  191.     return PyFloat_FromDouble(x);
  192. }
  193.  
  194. static void
  195. float_dealloc(op)
  196.     PyFloatObject *op;
  197. {
  198.     op->ob_type = (struct _typeobject *)free_list;
  199.     free_list = op;
  200. }
  201.  
  202. double
  203. PyFloat_AsDouble(op)
  204.     PyObject *op;
  205. {
  206.     PyNumberMethods *nb;
  207.     PyFloatObject *fo;
  208.     double val;
  209.     
  210.     if (op && PyFloat_Check(op))
  211.         return PyFloat_AS_DOUBLE((PyFloatObject*) op);
  212.     
  213.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  214.         nb->nb_float == NULL) {
  215.         PyErr_BadArgument();
  216.         return -1;
  217.     }
  218.     
  219.     fo = (PyFloatObject*) (*nb->nb_float) (op);
  220.     if (fo == NULL)
  221.         return -1;
  222.     if (!PyFloat_Check(fo)) {
  223.         PyErr_SetString(PyExc_TypeError,
  224.                 "nb_float should return float object");
  225.         return -1;
  226.     }
  227.     
  228.     val = PyFloat_AS_DOUBLE(fo);
  229.     Py_DECREF(fo);
  230.     
  231.     return val;
  232. }
  233.  
  234. /* Methods */
  235.  
  236. void
  237. PyFloat_AsStringEx(buf, v, precision)
  238.     char *buf;
  239.     PyFloatObject *v;
  240.     int precision;
  241. {
  242.     register char *cp;
  243.     /* Subroutine for float_repr and float_print.
  244.        We want float numbers to be recognizable as such,
  245.        i.e., they should contain a decimal point or an exponent.
  246.        However, %g may print the number as an integer;
  247.        in such cases, we append ".0" to the string. */
  248.     sprintf(buf, "%.*g", precision, v->ob_fval);
  249.     cp = buf;
  250.     if (*cp == '-')
  251.         cp++;
  252.     for (; *cp != '\0'; cp++) {
  253.         /* Any non-digit means it's not an integer;
  254.            this takes care of NAN and INF as well. */
  255.         if (!isdigit(Py_CHARMASK(*cp)))
  256.             break;
  257.     }
  258.     if (*cp == '\0') {
  259.         *cp++ = '.';
  260.         *cp++ = '0';
  261.         *cp++ = '\0';
  262.     }
  263. }
  264.  
  265. /* Precisions used by repr() and str(), respectively.
  266.  
  267.    The repr() precision (17 significant decimal digits) is the minimal number
  268.    that is guaranteed to have enough precision so that if the number is read
  269.    back in the exact same binary value is recreated.  This is true for IEEE
  270.    floating point by design, and also happens to work for all other modern
  271.    hardware.
  272.  
  273.    The str() precision is chosen so that in most cases, the rounding noise
  274.    created by various operations is suppressed, while giving plenty of
  275.    precision for practical use.
  276.  
  277. */
  278.  
  279. #define PREC_REPR    17
  280. #define PREC_STR    12
  281.  
  282. void
  283. PyFloat_AsString(buf, v)
  284.     char *buf;
  285.     PyFloatObject *v;
  286. {
  287.     PyFloat_AsStringEx(buf, v, PREC_STR);
  288. }
  289.  
  290. /* ARGSUSED */
  291. static int
  292. float_print(v, fp, flags)
  293.     PyFloatObject *v;
  294.     FILE *fp;
  295.     int flags; /* Not used but required by interface */
  296. {
  297.     char buf[100];
  298.     PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
  299.     fputs(buf, fp);
  300.     return 0;
  301. }
  302.  
  303. static PyObject *
  304. float_repr(v)
  305.     PyFloatObject *v;
  306. {
  307.     char buf[100];
  308.     PyFloat_AsStringEx(buf, v, PREC_REPR);
  309.     return PyString_FromString(buf);
  310. }
  311.  
  312. static PyObject *
  313. float_str(v)
  314.     PyFloatObject *v;
  315. {
  316.     char buf[100];
  317.     PyFloat_AsStringEx(buf, v, PREC_STR);
  318.     return PyString_FromString(buf);
  319. }
  320.  
  321. static int
  322. float_compare(v, w)
  323.     PyFloatObject *v, *w;
  324. {
  325.     double i = v->ob_fval;
  326.     double j = w->ob_fval;
  327.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  328. }
  329.  
  330. static long
  331. float_hash(v)
  332.     PyFloatObject *v;
  333. {
  334.     double intpart, fractpart;
  335.     int expo;
  336.     long x;
  337.     /* This is designed so that Python numbers with the same
  338.        value hash to the same value, otherwise comparisons
  339.        of mapping keys will turn out weird */
  340.  
  341. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  342. {
  343.     extended e;
  344.     fractpart = modf(v->ob_fval, &e);
  345.     intpart = e;
  346. }
  347. #else
  348.     fractpart = modf(v->ob_fval, &intpart);
  349. #endif
  350.  
  351.     if (fractpart == 0.0) {
  352.         if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
  353.             /* Convert to long int and use its hash... */
  354.             PyObject *w = PyLong_FromDouble(v->ob_fval);
  355.             if (w == NULL)
  356.                 return -1;
  357.             x = PyObject_Hash(w);
  358.             Py_DECREF(w);
  359.             return x;
  360.         }
  361.         x = (long)intpart;
  362.     }
  363.     else {
  364.         /* Note -- if you change this code, also change the copy
  365.            in complexobject.c */
  366.         long hipart;
  367.         fractpart = frexp(fractpart, &expo);
  368.         fractpart = fractpart * 2147483648.0; /* 2**31 */
  369.         hipart = (long)fractpart; /* Take the top 32 bits */
  370.         fractpart = (fractpart - (double)hipart) * 2147483648.0;
  371.                         /* Get the next 32 bits */
  372.         x = hipart + (long)fractpart + (long)intpart + (expo << 15);
  373.                         /* Combine everything */
  374.     }
  375.     if (x == -1)
  376.         x = -2;
  377.     return x;
  378. }
  379.  
  380. static PyObject *
  381. float_add(v, w)
  382.     PyFloatObject *v;
  383.     PyFloatObject *w;
  384. {
  385.     double result;
  386.     PyFPE_START_PROTECT("add", return 0)
  387.     result = v->ob_fval + w->ob_fval;
  388.     PyFPE_END_PROTECT(result)
  389.     return PyFloat_FromDouble(result);
  390. }
  391.  
  392. static PyObject *
  393. float_sub(v, w)
  394.     PyFloatObject *v;
  395.     PyFloatObject *w;
  396. {
  397.     double result;
  398.     PyFPE_START_PROTECT("subtract", return 0)
  399.     result = v->ob_fval - w->ob_fval;
  400.     PyFPE_END_PROTECT(result)
  401.     return PyFloat_FromDouble(result);
  402. }
  403.  
  404. static PyObject *
  405. float_mul(v, w)
  406.     PyFloatObject *v;
  407.     PyFloatObject *w;
  408. {
  409.     double result;
  410.  
  411.     PyFPE_START_PROTECT("multiply", return 0)
  412.     result = v->ob_fval * w->ob_fval;
  413.     PyFPE_END_PROTECT(result)
  414.     return PyFloat_FromDouble(result);
  415. }
  416.  
  417. static PyObject *
  418. float_div(v, w)
  419.     PyFloatObject *v;
  420.     PyFloatObject *w;
  421. {
  422.     double result;
  423.     if (w->ob_fval == 0) {
  424.         PyErr_SetString(PyExc_ZeroDivisionError, "float division");
  425.         return NULL;
  426.     }
  427.     PyFPE_START_PROTECT("divide", return 0)
  428.     result = v->ob_fval / w->ob_fval;
  429.     PyFPE_END_PROTECT(result)
  430.     return PyFloat_FromDouble(result);
  431. }
  432.  
  433. static PyObject *
  434. float_rem(v, w)
  435.     PyFloatObject *v;
  436.     PyFloatObject *w;
  437. {
  438.     double vx, wx;
  439.     double mod;
  440.     wx = w->ob_fval;
  441.     if (wx == 0.0) {
  442.         PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
  443.         return NULL;
  444.     }
  445.     PyFPE_START_PROTECT("modulo", return 0)
  446.     vx = v->ob_fval;
  447.     mod = fmod(vx, wx);
  448.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  449.        0 if wx < sqrt(smallest nonzero double) */
  450.     if (mod && ((wx < 0) != (mod < 0))) {
  451.         mod += wx;
  452.     }
  453.     PyFPE_END_PROTECT(mod)
  454.     return PyFloat_FromDouble(mod);
  455. }
  456.  
  457. static PyObject *
  458. float_divmod(v, w)
  459.     PyFloatObject *v;
  460.     PyFloatObject *w;
  461. {
  462.     double vx, wx;
  463.     double div, mod, floordiv;
  464.     wx = w->ob_fval;
  465.     if (wx == 0.0) {
  466.         PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
  467.         return NULL;
  468.     }
  469.     PyFPE_START_PROTECT("divmod", return 0)
  470.     vx = v->ob_fval;
  471.     mod = fmod(vx, wx);
  472.     /* fmod is typically exact, so vx-mod is *mathemtically* an
  473.        exact multiple of wx.  But this is fp arithmetic, and fp
  474.        vx - mod is an approximation; the result is that div may
  475.        not be an exact integral value after the division, although
  476.        it will always be very close to one.
  477.     */
  478.     div = (vx - mod) / wx;
  479.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  480.        0 if wx < sqrt(smallest nonzero double) */
  481.     if (mod && ((wx < 0) != (mod < 0))) {
  482.         mod += wx;
  483.         div -= 1.0;
  484.     }
  485.     /* snap quotient to nearest integral value */
  486.     floordiv = floor(div);
  487.     if (div - floordiv > 0.5)
  488.         floordiv += 1.0;
  489.     PyFPE_END_PROTECT(div)
  490.     return Py_BuildValue("(dd)", floordiv, mod);
  491. }
  492.  
  493. static double powu(x, n)
  494.     double x;
  495.     long n;
  496. {
  497.     double r = 1.;
  498.     double p = x;
  499.     long mask = 1;
  500.     while (mask > 0 && n >= mask) {
  501.         if (n & mask)
  502.             r *= p;
  503.         mask <<= 1;
  504.         p *= p;
  505.     }
  506.     return r;
  507. }
  508.  
  509. static PyObject *
  510. float_pow(v, w, z)
  511.     PyFloatObject *v;
  512.     PyObject *w;
  513.     PyFloatObject *z;
  514. {
  515.     double iv, iw, ix;
  516.     long intw;
  517.  /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
  518.   * The z parameter is really only going to be useful for integers and
  519.   * long integers.  Maybe something clever with logarithms could be done.
  520.   * [AMK]
  521.   */
  522.     iv = v->ob_fval;
  523.     iw = ((PyFloatObject *)w)->ob_fval;
  524.     intw = (long)iw;
  525.     if (iw == intw && -10000 < intw && intw < 10000) {
  526.         /* Sort out special cases here instead of relying on pow() */
  527.         if (intw == 0) {         /* x**0 is 1, even 0**0 */
  528.             PyFPE_START_PROTECT("pow", return 0)
  529.              if ((PyObject *)z!=Py_None) {
  530.                  ix=fmod(1.0, z->ob_fval);
  531.                  if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
  532.             }
  533.              else ix=1.0;
  534.             PyFPE_END_PROTECT(ix)
  535.                 return PyFloat_FromDouble(ix); 
  536.         }
  537.         errno = 0;
  538.         PyFPE_START_PROTECT("pow", return 0)
  539.         if (intw > 0)
  540.             ix = powu(iv, intw);
  541.         else
  542.             ix = 1./powu(iv, -intw);
  543.         PyFPE_END_PROTECT(ix)
  544.     }
  545.     else {
  546.         /* Sort out special cases here instead of relying on pow() */
  547.         if (iv == 0.0) {
  548.             if (iw < 0.0) {
  549.                 PyErr_SetString(PyExc_ValueError,
  550.                        "0.0 to a negative power");
  551.                 return NULL;
  552.             }
  553.             return PyFloat_FromDouble(0.0);
  554.         }
  555.         if (iv < 0.0) {
  556.             PyErr_SetString(PyExc_ValueError,
  557.                    "negative number to a float power");
  558.             return NULL;
  559.         }
  560.         errno = 0;
  561.         PyFPE_START_PROTECT("pow", return 0)
  562.         ix = pow(iv, iw);
  563.         PyFPE_END_PROTECT(ix)
  564.     }
  565.     CHECK(ix);
  566.     if (errno != 0) {
  567.         /* XXX could it be another type of error? */
  568.         PyErr_SetFromErrno(PyExc_OverflowError);
  569.         return NULL;
  570.     }
  571.      if ((PyObject *)z!=Py_None) {
  572.         PyFPE_START_PROTECT("pow", return 0)
  573.          ix=fmod(ix, z->ob_fval);    /* XXX To Be Rewritten */
  574.          if ( ix!=0 &&
  575.               ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
  576.              ix+=z->ob_fval;
  577.             }
  578.         PyFPE_END_PROTECT(ix)
  579.     }
  580.     return PyFloat_FromDouble(ix);
  581. }
  582.  
  583. static PyObject *
  584. float_neg(v)
  585.     PyFloatObject *v;
  586. {
  587.     return PyFloat_FromDouble(-v->ob_fval);
  588. }
  589.  
  590. static PyObject *
  591. float_pos(v)
  592.     PyFloatObject *v;
  593. {
  594.     Py_INCREF(v);
  595.     return (PyObject *)v;
  596. }
  597.  
  598. static PyObject *
  599. float_abs(v)
  600.     PyFloatObject *v;
  601. {
  602.     if (v->ob_fval < 0)
  603.         return float_neg(v);
  604.     else
  605.         return float_pos(v);
  606. }
  607.  
  608. static int
  609. float_nonzero(v)
  610.     PyFloatObject *v;
  611. {
  612.     return v->ob_fval != 0.0;
  613. }
  614.  
  615. static int
  616. float_coerce(pv, pw)
  617.     PyObject **pv;
  618.     PyObject **pw;
  619. {
  620.     if (PyInt_Check(*pw)) {
  621.         long x = PyInt_AsLong(*pw);
  622.         *pw = PyFloat_FromDouble((double)x);
  623.         Py_INCREF(*pv);
  624.         return 0;
  625.     }
  626.     else if (PyLong_Check(*pw)) {
  627.         *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
  628.         Py_INCREF(*pv);
  629.         return 0;
  630.     }
  631.     return 1; /* Can't do it */
  632. }
  633.  
  634. static PyObject *
  635. float_int(v)
  636.     PyObject *v;
  637. {
  638.     double x = PyFloat_AsDouble(v);
  639.     if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
  640.               : (x = floor(x)) > (double)LONG_MAX) {
  641.         PyErr_SetString(PyExc_OverflowError,
  642.                 "float too large to convert");
  643.         return NULL;
  644.     }
  645.     return PyInt_FromLong((long)x);
  646. }
  647.  
  648. static PyObject *
  649. float_long(v)
  650.     PyObject *v;
  651. {
  652.     double x = PyFloat_AsDouble(v);
  653.     return PyLong_FromDouble(x);
  654. }
  655.  
  656. static PyObject *
  657. float_float(v)
  658.     PyObject *v;
  659. {
  660.     Py_INCREF(v);
  661.     return v;
  662. }
  663.  
  664.  
  665. static PyNumberMethods float_as_number = {
  666.     (binaryfunc)float_add, /*nb_add*/
  667.     (binaryfunc)float_sub, /*nb_subtract*/
  668.     (binaryfunc)float_mul, /*nb_multiply*/
  669.     (binaryfunc)float_div, /*nb_divide*/
  670.     (binaryfunc)float_rem, /*nb_remainder*/
  671.     (binaryfunc)float_divmod, /*nb_divmod*/
  672.     (ternaryfunc)float_pow, /*nb_power*/
  673.     (unaryfunc)float_neg, /*nb_negative*/
  674.     (unaryfunc)float_pos, /*nb_positive*/
  675.     (unaryfunc)float_abs, /*nb_absolute*/
  676.     (inquiry)float_nonzero, /*nb_nonzero*/
  677.     0,        /*nb_invert*/
  678.     0,        /*nb_lshift*/
  679.     0,        /*nb_rshift*/
  680.     0,        /*nb_and*/
  681.     0,        /*nb_xor*/
  682.     0,        /*nb_or*/
  683.     (coercion)float_coerce, /*nb_coerce*/
  684.     (unaryfunc)float_int, /*nb_int*/
  685.     (unaryfunc)float_long, /*nb_long*/
  686.     (unaryfunc)float_float, /*nb_float*/
  687.     0,        /*nb_oct*/
  688.     0,        /*nb_hex*/
  689. };
  690.  
  691. PyTypeObject PyFloat_Type = {
  692.     PyObject_HEAD_INIT(&PyType_Type)
  693.     0,
  694.     "float",
  695.     sizeof(PyFloatObject),
  696.     0,
  697.     (destructor)float_dealloc, /*tp_dealloc*/
  698.     (printfunc)float_print, /*tp_print*/
  699.     0,            /*tp_getattr*/
  700.     0,            /*tp_setattr*/
  701.     (cmpfunc)float_compare, /*tp_compare*/
  702.     (reprfunc)float_repr,    /*tp_repr*/
  703.     &float_as_number,    /*tp_as_number*/
  704.     0,            /*tp_as_sequence*/
  705.     0,            /*tp_as_mapping*/
  706.     (hashfunc)float_hash,    /*tp_hash*/
  707.         0,            /*tp_call*/
  708.         (reprfunc)float_str,    /*tp_str*/
  709. };
  710.  
  711. void
  712. PyFloat_Fini()
  713. {
  714.     PyFloatObject *p;
  715.     PyFloatBlock *list, *next;
  716.     int i;
  717.     int bc, bf;    /* block count, number of freed blocks */
  718.     int frem, fsum;    /* remaining unfreed floats per block, total */
  719.  
  720.     bc = 0;
  721.     bf = 0;
  722.     fsum = 0;
  723.     list = block_list;
  724.     block_list = NULL;
  725.     free_list = NULL;
  726.     while (list != NULL) {
  727.         bc++;
  728.         frem = 0;
  729.         for (i = 0, p = &list->objects[0];
  730.              i < N_FLOATOBJECTS;
  731.              i++, p++) {
  732.             if (PyFloat_Check(p) && p->ob_refcnt != 0)
  733.                 frem++;
  734.         }
  735.         next = list->next;
  736.         if (frem) {
  737.             list->next = block_list;
  738.             block_list = list;
  739.             for (i = 0, p = &list->objects[0];
  740.                  i < N_FLOATOBJECTS;
  741.                  i++, p++) {
  742.                 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
  743.                     p->ob_type = (struct _typeobject *)
  744.                         free_list;
  745.                     free_list = p;
  746.                 }
  747.             }
  748.         }
  749.         else {
  750.             PyMem_FREE(list); /* XXX PyObject_FREE ??? */
  751.             bf++;
  752.         }
  753.         fsum += frem;
  754.         list = next;
  755.     }
  756.     if (!Py_VerboseFlag)
  757.         return;
  758.     fprintf(stderr, "# cleanup floats");
  759.     if (!fsum) {
  760.         fprintf(stderr, "\n");
  761.     }
  762.     else {
  763.         fprintf(stderr,
  764.             ": %d unfreed float%s in %d out of %d block%s\n",
  765.             fsum, fsum == 1 ? "" : "s",
  766.             bc - bf, bc, bc == 1 ? "" : "s");
  767.     }
  768.     if (Py_VerboseFlag > 1) {
  769.         list = block_list;
  770.         while (list != NULL) {
  771.             for (i = 0, p = &list->objects[0];
  772.                  i < N_FLOATOBJECTS;
  773.                  i++, p++) {
  774.                 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
  775.                     char buf[100];
  776.                     PyFloat_AsString(buf, p);
  777.                     fprintf(stderr,
  778.                  "#   <float at %lx, refcnt=%d, val=%s>\n",
  779.                         (long)p, p->ob_refcnt, buf);
  780.                 }
  781.             }
  782.             list = list->next;
  783.         }
  784.     }
  785. }
  786.